package gui;

import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.WindowConstants;
/**
 * the window to put our user name and connect to the network
 * link to the chat gui
 */
public class ConnectWindow extends JFrame implements ActionListener, KeyListener{
	private JLabel label;
	private JTextField username;
	private JButton bConnect;
	private ChatGUI chatGUI;
	
	public ConnectWindow(ChatGUI chatGUI) {
		this.chatGUI = chatGUI;
		this.setTitle("Connect Window");
		label = new JLabel("Username : ");
		username = new JTextField();
		bConnect = new JButton("Connect");
		bConnect.addActionListener(this);
		this.setLayout(new GridLayout(2,2));
		this.add(label);
		this.add(username);
		username.addKeyListener(this);
		this.add(bConnect);
		this.setFocusable(true);
		this.pack();
		this.setLocationRelativeTo(null);
		this.setVisible(true);
		setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);	
	}
	
	/**
	 * methods for ActionListener
	 */
	@Override
	public void actionPerformed(ActionEvent e) {
			//demander a chat controller de send hello a tous les machines en broadcast
			chatGUI.connect(username.getText());
			//ouvrir un userListWindow
			chatGUI.setUserList(chatGUI.getUserList()); //getUserList a creer
			this.dispose();
	}

	/**
	 * methods of KeyListener
	 */
	@Override
	public void keyPressed(KeyEvent key) {
		// TODO Auto-generated method stub
		if (key.getKeyCode() == KeyEvent.VK_ENTER){
			chatGUI.connect(username.getText());
			//ouvrir un userListWindow
			chatGUI.setUserList(chatGUI.getUserList()); //getUserList a creer
			this.dispose();	
		}
	}

	@Override
	public void keyReleased(KeyEvent e) {
		// TODO Auto-generated method stub
	}

	@Override
	public void keyTyped(KeyEvent e) {
		// TODO Auto-generated method stub
		
	}
}
